home *** CD-ROM | disk | FTP | other *** search
/ El Mac 9 / El Mac 9.iso / Shareware / Demos / Igor Demo Pro / 1 PutContentsIn Igor Pro Folder / WaveMetrics Procedures / Utilities / String Utilities / Value Report < prev    next >
Encoding:
Text File  |  1994-02-18  |  4.3 KB  |  133 lines  |  [TEXT/IGR0]

  1. | <Value Report>
  2. |
  3. |************
  4. | Given a base value (bval) plus the estimated error (sigma), returns a string
  5. |    containing the value rounded according to the error value.  Three parameters
  6. |    control the style of the result: method,unitsStr & flags.
  7. |
  8. |    method is an integer that selects a basic style.  Currently supported methods
  9. |    are:
  10. |        method==0    :    b.bbb
  11. |            prints just the base value rounded to the correct number of digits.
  12. |        method==1    :    b.bbb±s.sss
  13. |            prints both base and sigma values rounded.
  14. |        method==2    :    b.bbb (s.sss)
  15. |            same as method 1 except places sigma in parentheses
  16. |        method==3    :    b.bbb<ss>
  17. |            sigma is included as two (or one) digits that are understood to overlay
  18. |            the last two (or one) digits of the base value.
  19. |        method>=4    :    reserved for future expansion
  20. |
  21. |    Example:   23.45678 +/- 0.0987654 =>
  22. |        method==0    :     23.457
  23. |        method==1    :     23.457±0.099
  24. |        method==2    :     23.457 (0.099)
  25. |        method==3    :     23.457<99>
  26. |
  27. | If the unitsStr is non-null then SI prefixes are used if the value is not too large or
  28. |    too small.  Units are appended to the output string in any case.
  29. |
  30. |  Set bit 0 of flags if you want superscript exponential notation AND if you will
  31. |     be using the output in an annotation.  Don't set it if you are printing to history
  32. |     or to a file.
  33. | Set bit 1 of flags if you want 'E' style exponential notation.  Don't set both 0 & 1.
  34. | Set bit 2 of flags if you want rounding and/or sigma reporting to only 1 digit rather than 2.
  35. |
  36. | REQUIRES: IGOR PRO V2.0
  37. | MODIFICATIONS
  38. |    5-22-90,LH: added test for zero sigma
  39. |    11-11-93,LH: changed from 1.2 proc to 2.0 string function
  40. |    
  41. Function/S MakeValueReportString(bval,sigma,method,unitsStr,flags)
  42.     variable/D bval,sigma
  43.     variable method
  44.     string unitsStr                    | if not null, contains units (like V,s,W,m etc)
  45.     variable flags
  46.     string result= ""
  47.  
  48.     if( (method<0) %| (method>3) )
  49.         return "PMakeValueReportString: unknown method"
  50.     endif
  51.  
  52.     variable errordigits=2
  53.     if( flags %& 4 )
  54.         errordigits= 1
  55.     endif
  56.     
  57.     if( sigma==0 )                    | so something reasonable if zero sigma
  58.         sprintf result,"%g%s",bval,unitsStr
  59.         return result
  60.     endif
  61.     
  62.     variable bpwr= floor(log(max(sigma,abs(bval)))), spwr= floor(log(sigma))
  63.     
  64.     if( cmpstr(unitsStr, "") != 0 )
  65.         if( (bpwr >= 5) %| (bpwr <= -2) )                    | need prefixes at all?
  66.             variable  exponent= sign(bpwr)*ceil(abs(bpwr)/3)*3    | engineering units
  67.             if( (exponent >= -18) %& (exponent<=12) )    | in range for SI prefixes?
  68.                 sigma *= 10^-exponent
  69.                 bval *= 10^-exponent
  70.                 bpwr= floor(log(max(sigma,abs(bval)))); spwr= floor(log(sigma))
  71.                 unitsStr[0]= "afpnµm kMGT"[exponent/3+6]    | insert prefix from list
  72.             endif
  73.         endif
  74.     endif
  75.     variable ndigs= bpwr-spwr + errordigits -1
  76.     bval = round(bval*10^(ndigs-bpwr))*10^-ndigs
  77.     if( method==3 )
  78.         sigma = round(sigma*10^(errordigits -1 -spwr))
  79.     else
  80.         sigma = round(sigma*10^(ndigs-bpwr))*10^-ndigs
  81.     endif
  82. |printf "bval=%g, sigma=%g,bpwr=%d,spwr=%d,ndigs=%d\r",bval,sigma,bpwr,spwr,ndigs    
  83.     if( (bpwr < 5) %& (bpwr >= -2) )                        | try to avoid exponential mode
  84.         bval *= 10^bpwr                                    | undo scaling on bval
  85.         if( method!=3 )
  86.             sigma *= 10^bpwr                                | undo scaling on sigma
  87.         endif
  88.         if( bpwr >= ndigs )
  89.             if( method==3 )
  90.                 sigma *= 10^(bpwr-ndigs)                    | zero pad sigma
  91.             endif
  92.             ndigs= 0
  93.         else
  94.             ndigs -= bpwr
  95.         endif
  96.         if( method==0 )
  97.             sprintf result,"%.*f%s",ndigs,bval,unitsStr
  98.         endif
  99.         if( method==1 )
  100.             sprintf result,"%.*f±%.*f %s",ndigs,bval,ndigs,sigma,unitsStr
  101.         endif
  102.         if( method==2 )
  103.             sprintf result,"%.*f (%.*f) %s",ndigs,bval,ndigs,sigma,unitsStr
  104.         endif
  105.         if( method==3 )
  106.             sprintf result,"%.*f<%d>%s",ndigs,bval,sigma,unitsStr
  107.         endif
  108.     else                                    | exponential mode
  109.         String spfstr= "x10^%d"        | default format for exponent
  110.         if(flags %& 1)
  111.             spfstr= "x10\S%d\M"        | superscript format for exponent;    BUG: should need double "\"
  112.         endif
  113.         if(flags %& 2)
  114.             spfstr= "E%d"                | E format for exponent
  115.         endif
  116.  
  117.         if( method==0 )
  118.             sprintf  result,"%.*f"+spfstr+"%s",ndigs,bval,bpwr,unitsStr
  119.         endif
  120.         if( method==1 )
  121.             sprintf  result,"%.*f"+spfstr+"±%.*f"+spfstr+" %s",ndigs,bval,bpwr,ndigs,sigma,bpwr,unitsStr
  122.         endif
  123.         if( method==2 )
  124.             sprintf  result,"%.*f"+spfstr+" (%.*f"+spfstr+") %s",ndigs,bval,bpwr,ndigs,sigma,bpwr,unitsStr
  125.         endif
  126.         if( method==3 )
  127.             sprintf  result,"%.*f<%d>"+spfstr+"%s",ndigs,bval,sigma,bpwr,unitsStr
  128.         endif
  129.     endif
  130. |printf "bval=%s\r",result
  131.     return result
  132. end
  133.